package cn.initcap.algorithm.swing.fractal.recursivecircle;

import cn.initcap.algorithm.swing.AlgoVisHelper;
import cn.initcap.algorithm.swing.BaseAlgoFrame;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author initcap
 * @date 8/1/19 10:40 AM
 */
public class AlgoVisualizer {

    private static int DELAY = 40;
    private CircleData data;
    private BaseAlgoFrame frame;

    public AlgoVisualizer(int sceneWidth, int sceneHeight) {

        int r = Math.min(sceneWidth, sceneHeight) / 2 - 2;
        data = new CircleData(sceneWidth / 2, sceneHeight / 2, r, r / 2, 2);

        EventQueue.invokeLater(() -> {
            frame = new BaseAlgoFrame("Fractal Visualizer", sceneWidth, sceneHeight) {
                @Override
                public void draw(Graphics graphics) {
                    Graphics2D g2d = (Graphics2D) graphics;
                    drawCircle(g2d, data.getStartX(), data.getStartY(), data.getStartR(), 0);
                }
            };

            new Thread(() -> run()).start();
        });
    }

    public static void main(String[] args) {

        int sceneWidth = 800;
        int sceneHeight = 800;

        new AlgoVisualizer(sceneWidth, sceneHeight);
    }

    private void drawCircle(Graphics2D g, int x, int y, int r, int depth) {

        if (depth == data.getDepth() || r < 1) {
            return;
        }

        if (depth % 2 == 0) {
            AlgoVisHelper.setColor(g, AlgoVisHelper.RED);
        } else {
            AlgoVisHelper.setColor(g, AlgoVisHelper.LIGHT_BLUE);
        }

        AlgoVisHelper.fillCircle(g, x, y, r);
        drawCircle(g, x, y, r - data.getStep(), depth + 1);
    }

    private void run() {

        setData();

    }

    private void setData() {

        frame.render(data);
        AlgoVisHelper.pause(DELAY);
    }
}